﻿using DBManager;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace FormMdiTest
{
    /// <summary>
    /// 过滤报警明细项目
    /// </summary>
    public class DurFilterItem
    {
        public long nFacilityID { get; set; }
        public string strDurCode { get; set; }           //报警类型
        public string strDrugCategoryType { get; set; }  //DRUG_ID, DRUG_CATEGORY
        public string strDrugCategoryValue { get; set; } //
        public int nSeverityThreshold { get; set; }      //过滤的报警级别
        public int nPatientMinAgeMonth { get; set; }     //过滤的患者最小年龄（月）
        public int nPatientMaxAgeMonth { get; set; }     //过滤的患者最大年龄（月）
        public string strDDDrugCategoryType { get; set; }  //DRUG_ID, DRUG_CATEGORY
        public string strDDDrugCategoryValue { get; set; } //
        public string strMODECODE { get; set; }            //:EXCLUDE/DENY

        public string strDiagnosisCodeType { set; get; }  //
        public string strDiagnosisCode { get; set; } //
        public string strDiagnosisName { get; set; } //

        public string strDrugRXType { get; set; }    //LONG/TEMPORARY/ DISCHARGE (长嘱，临嘱，出院带药)
        public string strDDDrugRXType { get; set; }  //LONG/TEMPORARY/ DISCHARGE (长嘱，临嘱，出院带药)

        public string strFilterPatientTypeCode { get; set; }     //OUTPATIENT / INPATIENT  (门诊,住院)，可以只在门诊过滤，或只在住院过滤
        public string strFilterHinPhyIdent { get; set; }
        public string strFilterPhyName { get; set; }
        public string strFilterPhyClass { get; set; }
        public string strFilterDepartmentCode { get; set; }
        public string strFilterDepartmentName { get; set; }

        public string strFilterRouteCode { get; set; }



    }
    public class DurFilterCatch
    {
        private Dictionary<string, List<DurFilterItem>> filterDic = new Dictionary<string, List<DurFilterItem>>();


        /// <summary>
        /// 获取数据库中所有的报警过滤数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetDurFilterData()
        {
            DBResult dbDataResult;
            DataTable dtDataRet;

            string strSQL = "SELECT [DUR_CODE] AS strDurCode " +
                          ",[DRUG_CATEGORY_TYPE] AS strDrugCategoryType " +
                          ",[DRUG_CATEGORY_VALUE] AS strDrugCategoryValue " +
                          ",[SEVERITY_THRESHOLD] AS nSeverityThreshold " +
                          ",[MIN_AGE_MONTHS] AS nPatientMinAgeMonth " +
                          ",[MAX_AGE_MONTHS] AS nPatientMaxAgeMonth " +
                          ",[DD_DRUG_CATEGORY_TYPE] AS strDDDrugCategoryType " +
                          ",[DD_DRUG_CATEGORY_VALUE] AS strDDDrugCategoryValue " +
                          ",[MODE_CODE] strMODECODE " +
                          ",[FACILITY_ID] AS nFacilityID "  +
                          ",[DIAGNOSIS_CODE_TYPE] AS strDiagnosisCodeType " +
                          ",[DRUG_RX_TYPE] AS strDrugRXType " +
                          ",[DD_DRUG_RX_TYPE] AS strDDDrugRXType " +
                          ",[FILTER_PATIENT_TYPE_CODE] AS strFilterPatientTypeCode " +
                          ",[FILTER_HIN_PHYS_IDENT] AS strFilterHinPhyIdent " +
                          ",[FILTER_PHYS_NAME] AS strFilterPhyName " +
                          ",[FILTER_PHYS_CLASS] AS strFilterPhyClass " +
                          ",[FILTER_DEPARTMENT_CODE] AS strFilterDepartmentCode " +
                          ",[FILTER_DEPARTMENT_NAME] AS strFilterDepartmentName " +
                          ",[DIAGNOSIS_CODE] AS strDiagnosisCode " +
                          ",[DIAGNOSIS_NAME] AS strDiagnosisName " +
                          ",[ROUTE_CODE]  AS strFilterRouteCode " +
                      "FROM [dbo].[DUR_FILTER]";
            dbDataResult = DBReader.GetDataTable("LocalDB", strSQL);
            return dtDataRet = dbDataResult.dtTable;
        }
        public void ResetCatch()
        {
            //1.清理所有存储数据
            filterDic.Clear();
            //2.遍历添加数据
            DataTable dtFilter = GetDurFilterData();
            List<DurFilterItem> filterItems = ConvertDataTableToList<DurFilterItem>(dtFilter);
            //3.按报警类别分组装箱
            var filterGroups = filterItems.GroupBy((f) => f.strDurCode);
            foreach (var group in filterGroups)
            {
                Dictionary<string,DurFilterItem> dicFilter = group.ToDictionary((f) => f.strDurCode);
            }

            //foreach(DurFilterItem item in filterItems)
            //{
            //    string durCode = item.strDurCode;
            //    if (!filterDic.ContainsKey(durCode))
            //    {
            //        filterDic.Add(durCode, new List<DurFilterItem>());
            //        List<DurFilterItem> listFilter = (List<DurFilterItem>)filterDic[durCode];
            //    }
            //}
        }

        private List<T> ConvertDataTableToList<T>(DataTable dt) where T : new()
        {
            var ts = new List<T>();
            Type type = typeof(T);
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    //T t = default(T);
                    T t = System.Activator.CreateInstance<T>();
                    System.Reflection.PropertyInfo[] properties = type.GetProperties();
                    foreach (var item in properties)
                    {
                        if (!item.CanWrite) continue;
                        if (dt.Columns.Contains(item.Name)&&!Convert.IsDBNull(row[item.Name]))
                            item.SetValue(t, row[item.Name]??"", null);
                    }
                    ts.Add(t);
                }
            }
            catch (Exception ex)
            {
                //LogManager.Logger.Write("DataTableConvertToList  IS ERROR #### 具体原因:" + ex.Message);
            }
            return ts;
        }


    }

}
